#!/bin/bash

# 退出码检查
sure() {
    eval $*
    err=$?
    if [ "$err" != "0" ]
    then
        echo "ERROR during : $*"
        echo "ERROR during : $*: $err" > last_error
        exit 1
    fi
}

PKGBUILD_ROOT=$(cd $(dirname ${BASH_SOURCE[0]}); pwd)

# 下载库压缩包
# 参数1 链接地址
# 参数2 压缩包名
download() {
    if [ -s ${PWD}/$2 ]
    then
        echo ${PWD}/$2"，存在"
    else
        if [ ! -f $LYCIUM_CACHE/$2 ];then
            echo "Downloading "$2
            wget "$1" -O $LYCIUM_CACHE/$2 -o download.log
        else
            echo "using cache "$2
        fi
        ln -fs $LYCIUM_CACHE/$2 ${PWD}/$2
        return $?
    fi
}

# 函数：克隆 Git 仓库（带缓存优化）
# 参数:
#   $1 - Git 仓库 URL
#   $2 - 构建目录路径
#   $3 - 日志文件路径 (可选)
#   $4 - 日志文件路径 (可选)
clone_repo() {
    local url="$1"
    local builddir="$2"
    local branch="$3"
    local publicbuildlog="${4:-/dev/null}"  # 默认日志路径

    # 验证必需参数
    if [[ -z "$url" || -z "$builddir" ]]; then
        echo "Error: URL and build directory are required" >&2
        return 1
    fi

    # 获取仓库名（用于缓存路径）
    local repo_name=$(basename "$url" .git)
    local cache_path="${LYCIUM_CACHE}/${repo_name}"

    # 第一步：尝试使用缓存
    if [[ -n "$LYCIUM_CACHE" ]] && [[ -d "$cache_path" ]]; then
        echo "Using cached repository at: $cache_path"
        if ! cp -R "${cache_path}/." "$builddir"; then
            echo "Error: Failed to copy from cache to $builddir" >&2
            return 1
        fi
        echo "Successfully copied from cache"
        return 0
    fi


    # 创建缓存目录
    mkdir -p "$cache_path"
    if [[ -n "$branch" ]]; then
        # 先克隆到缓存位置
        if ! git clone -b "${branch}" "${url}" "$cache_path" >> "$publicbuildlog" 2>&1; then
            echo "Error: Failed to clone repository to cache" >&2
            rm -rf $cache_path
            return 1
        fi
    else
        if ! git clone "${url}" "$cache_path" >> "$publicbuildlog" 2>&1; then
            echo "Error: Failed to clone repository to cache" >&2
            rm -rf $cache_path
            return 1
        fi
    fi
    # 复制到目标位置
    if ! cp -R "${cache_path}/." "$builddir"; then
        echo "Error: Failed to copy cached repo to $builddir" >&2
        return 1
    fi

    echo "Repository successfully prepared"
    return 0
}

# 解压库
# 参数1 压缩包名
unpack() {
    if [ -f ${PWD}/$1 ]
    then
        if [[ "$1" == *.tar.gz ]]
        then
            tar -zxvf ${PWD}/$1 > /dev/null
        elif [[ "$1" == *.tgz ]]
        then
            tar -zxvf ${PWD}/$1 > /dev/null
        
        elif [[ "$1" == *.tar.xz ]]
        then
            tar -xvJf ${PWD}/$1 > /dev/null
        elif [[ "$1" == *.tar.bz2 ]]
        then
            tar -xvjf ${PWD}/$1 > /dev/null
        elif [[ "$1" == *.zip ]]
        then
            unzip ${PWD}/$1 > /dev/null
        else
            echo "ERROR Package Format!"
            exit 2
        fi
    else
        echo "ERROR Package Not Found!"
        exit 2
    fi
}

# 加载库信息
source ${PWD}/HPKBUILD

# 库的完整性校验
checksum() {
    sha512sum -c ${PWD}/$1
    ret=$?
    return $ret
}

newdeps=()
builddepends() {
    donelist=($*)
    # 如果有依赖没有编译完，则跳过编译后续再次编译
    deplen=${#depends[*]}
    count=0
    for depend in ${depends[@]}
    do
        doneflag=false
        for donelib in ${donelist[@]}
        do
            if [ $depend == $donelib ]
            then
                count=$((count+1))
                doneflag=true
            fi
        done
        # 记录未编译的依赖项
        # echo $doneflag
        if ! $doneflag
        then
            # echo -----------
            newdeps[${#newdeps[@]}]=$depend
        fi
    done
    if [ $count -ne $deplen ]
    then
        return 101
    fi
    return 0
}

recordbuildlibs() {
    echo $2,$3,$1,$4>> $LYCIUM_ROOT/$CMAKE_BUILD_TYPE/hpk_build.csv
}

buildargs=
pkgconfigpath=
cmakedependpath() { # 参数1为cpu type
    buildargs="-LH -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_SKIP_RPATH=ON -DCMAKE_SKIP_INSTALL_RPATH=ON -DCMAKE_TOOLCHAIN_FILE=${OHOS_SDK}/native/build/cmake/ohos.toolchain.cmake -DCMAKE_INSTALL_PREFIX=$LYCIUM_ROOT/$CMAKE_BUILD_TYPE/$pkgname/$1/ -G \"Unix Makefiles\" -DOHOS_ARCH=$1 "
    pkgconfigpath=""
    if [ ${#depends[@]} -ne 0 ] 
    then
        tmppath="\""
        for depend in ${depends[@]}
        do
            dependpath=$LYCIUM_ROOT/$CMAKE_BUILD_TYPE/$depend/$1/
            tmppath=$tmppath"${dependpath};"

            dependpkgpath=$LYCIUM_ROOT/$CMAKE_BUILD_TYPE/$depend/$1/lib/pkgconfig
            if [ -d ${dependpkgpath} ]
            then
                pkgconfigpath=$pkgconfigpath"${dependpkgpath}:"
            fi
        done
        tmppath=${tmppath%;*}
        pkgconfigpath=${pkgconfigpath%:*}
        tmppath=$tmppath"\""
        buildargs=$buildargs"-DCMAKE_FIND_ROOT_PATH="$tmppath
    fi
}

configuredependpath() {
    pkgconfigpath=""
    buildargs="--prefix=$LYCIUM_ROOT/$CMAKE_BUILD_TYPE/$pkgname/$1/"
    if [ ${#depends[@]} -ne 0 ] 
    then
        for depend in ${depends[@]}
        do
            dependpath=$LYCIUM_ROOT/$CMAKE_BUILD_TYPE/$depend/$1/lib/pkgconfig
            if [ ! -d ${dependpath} ]
            then
                continue
            fi
            pkgconfigpath=$pkgconfigpath"${dependpath}:"
        done
        pkgconfigpath=${pkgconfigpath%:*}
    fi
}

checkmakedepends() {
    ismakedependready=true
    for makedepend in ${makedepends[@]}
    do
        which $makedepend >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
            echo "请先安装 $makedepend 命令, 才可以编译 $1"
            ismakedependready=false
        fi
    done
    if ! $ismakedependready
    then
        echo "!!! 退出 $1 编译 !!!"
        exit 1
    fi
}

buildlog=
publicbuildlog=
builpackage() {
    donelist=($*)
    builddepends "${donelist[*]}"
    if [ $? -eq 101 ]
    then
        echo $pkgname" not ready. wait "${newdeps[*]}
        for dep in ${newdeps[@]}
        do
            echo $dep >> ${LYCIUM_DEPEND_PKGNAMES}
        done
        exit 101
    fi
    echo "Start building $pkgname $pkgver!"
    if [ ! $downloadpackage ] || [ $downloadpackage != false ]
    then
        sure download $source $packagename
        if [ -f "SHA512SUM" ]
        then
            # 判断新下载的源码压缩包正确性。如果失败退出编译，不删除，下载的压缩包
            checksum SHA512SUM
            ret=$?
            if [ $ret -ne 0 ]
            then
                echo "SHA512SUM 校验失败, 请确认 SHA512SUM 无误后, 重新编译"
                exit $ret
            fi
        fi
    fi
    if [ ! $autounpack ] || [ $autounpack != false ]
    then
        sure unpack $packagename
    fi
    
    checkmakedepends $pkgname
    for arch in ${archs[@]}
    do
        # 只编译arm64-v8a
        if [ "$arch" != "arm64-v8a" ]; then
            echo "跳过$arch"
            continue
        fi
        if [ ! $buildtools ] || [ $buildtools == "cmake" ]
        then
            echo "编译cmake $pkgname-$buildtools"
        else
            echo "跳过$buildtools"
            continue
        fi
        # TODO archs1 编译失败，继续编译archs2
        echo "Compileing OpenHarmony $arch $pkgname $pkgver libs..." 
        ARCH=$arch
        buildlog=$PKGBUILD_ROOT/$pkgname-$pkgver-$ARCH"-lycium_build.log"
        publicbuildlog=$PKGBUILD_ROOT/$pkgname"-public-lycium_build.log"
        sure prepare
        if [ ! $buildtools ] || [ $buildtools == "cmake" ]
        then
            sure cmakedependpath $ARCH
        elif [ $buildtools == "configure" ]
        then
            sure configuredependpath $ARCH
        else
            :
        fi
        sure build $buildargs
        sure package
        if [ -n "${LYCIUM_BUILD_CHECK}" ]
        then
            if [ ${LYCIUM_BUILD_CHECK} == "true" ]
            then
                sure check
            fi
        fi
        f=`type -t recoverpkgbuildenv`
        if [ "x$f" = "xfunction" ]
        then
            sure recoverpkgbuildenv
        fi
        sure recordbuildlibs $ARCH $pkgname $pkgver $buildtools
    done
    echo "Build $pkgname $pkgver end!"
}

cleanhpk() {
    sure cleanbuild
    # 提前校验已存在的压缩包, 非法则清理. 待后续新下载
    if [ -s ${PWD}/$packagename ]
    then
        if [ -f "SHA512SUM" ]
        then
            checksum SHA512SUM
            ret=$?
            if [ $ret -ne 0 ]
            then
                rm -f ${PWD}/$packagename
            fi
        fi
    fi
    rm -rf *-lycium_build.log
}

main() {
    # 清理上次的环境
    sure cleanhpk
    # 编译 PKG
    sure builpackage $*
}

main $*
